package promogui;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.Ostermiller.util.BadDelimiterException;
import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;

@SuppressWarnings("unused")
public class CSVIO
{
	
	public static void main(String[] args)
	{	
		try {
			
			//CSVIO csv = new CSVIO("process_data.csv");
			// csvFile = new LabeledCSVParser(new CSVParser(new FileInputStream(filename), ';'));
			
			ArrayList<Project> projects;
			
			projects = CSVIO.getProjects();
			
			Iterator<Project> it = projects.iterator();
			
			while(it.hasNext())
			{
				
				Project project = it.next();
				
				float currentEstimated = project.getCurrentTotalEstimated();
				float currentWorked = project.getTotalWorked();
				float totalEstimated = project.getTotalEstimated();
				
				
				float prozent =  100 - ((currentEstimated - currentWorked) / totalEstimated) * 100;
				
				
				System.out.println(project.getName());
				System.out.println("Estimated: " + project.getTotalEstimated());
				System.out.println("Current Estimated: " + project.getCurrentTotalEstimated());
				System.out.println("Worked: " + project.getTotalWorked());
				System.out.println("Week: " + project.getCurrentWeek());
				System.out.println("Percent: " + prozent);
            
            ArrayList<Integer> loc = project.getLinesOfCode();
            
            System.out.print("Lines Of Code: ");
            
            for (int i = 0; i < loc.size(); i++)
            {
                System.out.print( loc.get(i) + " - ");
            }
            
            System.out.println();
            
				System.out.println();
				
				//System.out.println(project.getTotalWorkedAD());
			}
			
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
	
	private static float getValueByLabel(LabeledCSVParser csvFile, String label)
	{
		if (csvFile.getValueByLabel(label).isEmpty())
		{
			return 0;
		}
		else
		{	
			return Float.parseFloat(csvFile.getValueByLabel(label));
		}		
	}
	
   private static HashMap<String, HashMap<Integer, WeeklyProductData>> getWeeklyProductData(String filename) throws NumberFormatException, IllegalStateException, IOException
   {
       
        HashMap<String, HashMap<Integer, WeeklyProductData>> weeklyProductData = new HashMap<String, HashMap<Integer, WeeklyProductData>>();
        
        LabeledCSVParser csvFile = new LabeledCSVParser(new CSVParser(new FileInputStream(filename), ';'));

        while(csvFile.getLine() != null)
        {
            String projectID;

            projectID = csvFile.getValueByLabel("Project-ID");
            int weekNo = Integer.parseInt(csvFile.getValueByLabel("Week"));       
  
            HashMap<Integer, WeeklyProductData> projectWeeks = weeklyProductData.get(projectID);
            if (projectWeeks == null)
            {
                projectWeeks = new HashMap<Integer, WeeklyProductData>();
                weeklyProductData.put(projectID, projectWeeks);
            }
            
            int not, nob, loc, noc, notc, post, pocc;
            
            not = (int) getValueByLabel(csvFile, "NoT");
            nob = (int) getValueByLabel(csvFile, "NoB");
            loc = (int) getValueByLabel(csvFile, "LoC");
            noc = (int) getValueByLabel(csvFile, "NoC");
            notc = (int) getValueByLabel(csvFile, "NoTC");
            post = (int) getValueByLabel(csvFile, "PoST");
            pocc = (int) getValueByLabel(csvFile, "PoCC");
            
            WeeklyProductData weekProductData = new WeeklyProductData(weekNo, not, nob, loc, noc, notc, post, pocc);
            
            projectWeeks.put(weekNo, weekProductData);
        }
        
        return weeklyProductData;
    }
   
    private static HashMap<String, HashMap<Integer, WeeklyProjectData>> getWeeklyProjectData(String filename) throws NumberFormatException, IllegalStateException, IOException
	{
		HashMap<String, HashMap<Integer, WeeklyProjectData>> weeklyProjectData = new HashMap<String, HashMap<Integer, WeeklyProjectData>>();
		HashMap<String, Engineer> engineers = new HashMap<String, Engineer>();
		
		LabeledCSVParser csvFile = new LabeledCSVParser(new CSVParser(new FileInputStream(filename), ';'));
		
		while(csvFile.getLine() != null)
		{
			String projectID, engineerID;
			
			projectID = csvFile.getValueByLabel("Project-ID");
			engineerID = csvFile.getValueByLabel("Dev-ID");
			int weekNo = Integer.parseInt(csvFile.getValueByLabel("Week"));
			
			HashMap<Integer, WeeklyProjectData> projectWeeks = weeklyProjectData.get(projectID);
			if (projectWeeks == null)
			{
				projectWeeks = new HashMap<Integer, WeeklyProjectData>();
				weeklyProjectData.put(projectID, projectWeeks);
			}
			
			WeeklyProjectData weeklyData = projectWeeks.get(weekNo);
			if (weeklyData == null)
			{
				weeklyData = new WeeklyProjectData(weekNo);
				projectWeeks.put(weekNo, weeklyData);
			}
			
			Engineer engineer = engineers.get(engineerID);
			if (engineer == null)
			{
				engineer = new Engineer(engineerID);
				engineers.put(engineerID, engineer);
			}
				
			float re, ad, ii, bt, pm;
			
			re = getValueByLabel(csvFile, "RE");
			ad = getValueByLabel(csvFile, "AD");
			ii = getValueByLabel(csvFile, "II");
			bt = getValueByLabel(csvFile, "BT");
			pm = getValueByLabel(csvFile, "PM");		

			WeeklyEngineerData data = new WeeklyEngineerData(re, ad, ii, bt, pm, engineer);
			
			weeklyData.addWeeklyEngineerData(data);      			
		}		
		
		return weeklyProjectData;
	}
	
	public static ArrayList<Project> getProjects() throws FileNotFoundException, IOException
	{
		ArrayList<Project> projects = new ArrayList<Project>();
      ArrayList<Project> projectsReverse = new ArrayList<Project>();
		HashMap<String, HashMap<Integer, WeeklyProjectData>> plannedData = getWeeklyProjectData("planned_data2.csv");
		HashMap<String, HashMap<Integer, WeeklyProjectData>> processData = getWeeklyProjectData("process_data2.csv");
		HashMap<String, HashMap<Integer, WeeklyProductData>> productData = getWeeklyProductData("product_data2.csv");
      
		//Iterator<HashMap<Integer, WeeklyProjectData>> it = plannedData.iterator();
		
		for (String projectName : plannedData.keySet())
		{
			Project project = new Project(projectName);
			HashMap<Integer, WeeklyProjectData> projectsPlannedData = plannedData.get(projectName);
			HashMap<Integer, WeeklyProjectData> projectsProcessData = processData.get(projectName);
			HashMap<Integer, WeeklyProductData> projectsProductData = productData.get(projectName);
         
			ArrayList<WeeklyProjectData> projectEstimated = new ArrayList<WeeklyProjectData>();
			
			for (int i = 0; i < projectsPlannedData.size(); i++)
			{
				projectEstimated.add( projectsPlannedData.get(i+1) );
			}
			
			project.setArrayListEstimated(projectEstimated);
			
			ArrayList<WeeklyProjectData> projectTotal = new ArrayList<WeeklyProjectData>();
			
			for (int i = 0; i < projectsProcessData.size(); i++)
			{
				projectTotal.add( projectsProcessData.get(i+1) );
			}
			
			project.setArrayListTotal(projectTotal);
         
         ArrayList<WeeklyProductData> productDataList = new ArrayList<WeeklyProductData>();
         
         for (int i = 0; i < projectsProductData.size(); i++)
         {
             productDataList.add(projectsProductData.get(i+1));
         }
         
         project.setArrayListProductData(productDataList);
			
			projects.add(project);
			
			//System.out.println(projectName);
		}
      
      for (int i = projects.size() - 1; i >= 0; i--)
      {
          projectsReverse.add(projects.get(i));
      }
		
		return projectsReverse;
	}
}
